namespace music {
	export class Sounds extends BaseMgr {
		isOpen = true;

		volume = 0.3;

		private static instance: Sounds;

		public static ins(): Sounds {
			if (!this.instance) {
				this.instance = new Sounds('Sounds');
			}
			return this.instance;
		}

		findSound(file: string): egret.Sound {
			return this.findCache(file);
		}

		async getSound(file: string): Promise<egret.Sound> {
			let sound = this.findSound(file);
			if (sound) {
				return sound;
			}

			sound = await loadSoundByUrl(file);
			if (sound) {
				this.addCache(file, sound);
			}
			return sound;
		}

		async playOnce(
			file: string,
			vol: number,
			callback?: dramaCallback,
		): Promise<egret.SoundChannel> {
			const sound = await this.getSound(file);
			if (!sound) {
				return null;
			}
			const channel = sound.play(0, 1);
			channel.volume = this.volume * vol;
			if (callback) {
				channel.once(
					egret.Event.SOUND_COMPLETE,
					() => {
						callback(1);
					},
					null,
				);
			}
			return channel;
		}

		setVolume(vol: number): void {
			this.volume = vol;
		}

		onActive(): void {
			this.isOpen = true;
		}

		onDeactive(): void {
			this.isOpen = false;
		}
	}

	// 简单的播放一个声音一次！不能loop! vol [0,1]
	export function playOnce(file: string, vol: number, callback?: dramaCallback): void {
		if (!gamedata.Setting.ins().isSoundon()) {
			if (callback) {
				callback(1);
			}
			return;
		}
		Sounds.ins().playOnce(`sound/${file}`, vol, callback);
	}

	export function playEvent(file: string): void {
		const [file2, vol] = file.split(':');
		playOnce(file2, Number(vol));
	}

	// // 简单的播放一个声音一次！不能loop! vol [0,1]
	// export function playOnce(
	// 	file: string,
	// 	vol: number,
	// 	callback?: dramaCallback,
	// ): void {
	// 	if (!gamedata.Setting.ins().isSoundon()) {
	// 		return;
	// 	}
	// 	Sounds.ins().playOnce(`sound/${file}`, vol, callback);
	// }

	// export function playEvent(file: string): void {
	// 	const [file2, vol] = file.split(':');
	// 	const vol2 = vol ? Number(vol) : 0.5;
	// 	playOnce(file2, vol2, null);
	// }
}
